C#中"+"的应用

来源:百度知道 编辑:UC知道 时间:2024/05/30 05:50:10
namespace WindowsApplication7
{
public partial class Login : Form
{

public Login()
{
InitializeComponent();
}

private void Login_Load(object sender, EventArgs e)
{

Random ran = new Random();
label2.Text = ran.Next(1000, 10000).ToString();
}

private void btnLogin_Click(object sender, EventArgs e)
{
//string Userpwd = UserPwd.Text;
OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\jiayu9394\\My Documents\\数据库\\酒店管理数据库11.mdb");
OleDbCommand cmd = new OleDbCommand("select * from admin where ID='" + UserID.Text + "'", conn);
conn.

应该这样看
("select * from admin where ID='" + UserID.Text + "'", conn);
("select * from admin where ID='"第一部分
UserID.Text 第二部分
"'" 第三部分
等价于
("select * from admin where ID='" + "abcef"+ "'", conn);
即select * from admin where ID='abcef'
不是你说的”+ UserID.Text +”

简单的说是用来字符串相拼接

数据库中可以直接'常量' 如果用c#连接的话 必须要用'"+变量+"' 如果是个常量的话也可以'常量'
或者 先字符串格式化一下string str=string.Format("select * from admin where ID='{0}'",UserID.Text);
然后:OleDbCommand cmd = new OleDbCommand(str, conn);

这么简单都不会~